from math import gcd
n,m=map(int,input().split())
gcdd=gcd(n,m)
l=list(map(int,input().split()));r=[0]*gcdd
for i in l[1:]:
r[i%gcdd]=1
l1=list(map(int,input().split()))
for j in l1[1:]:
r[j%gcdd]=1
for i in r:
if i==0:exit(print("No"))
print("Yes")
// radhe Krishna //
#include<bits/stdc++.h>
using namespace std;
#define nl "\n"
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define al(x) x.begin(), x.end()
#define Test int t;cin>>t;while(t--)
#define rep(i,n) for(ll i=0;i<n;i++)
#define mpp map<ll,ll>
#define vi(type) vector<type>
#define vv vector<vector<ll>>
#define vp vector<pair<ll,ll>>
#define uq(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
ll dx[4]={-1,0,1,0};
ll dy[4]={0,1,0,-1};
void fast()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void solve(){
ll n,m; cin>>n>>m;
vi(ll) a(n),b(m);
mpp mpa,mpb;
ll by; cin>>by;
rep(i,by) {
cin>>a[i];
mpa[a[i]]=1;
}
ll gy; cin>>gy;
rep(i,gy) {
cin>>b[i];
mpb[b[i]]=1;
}
for(ll i=0;i<10000;i++){
if(mpa[i%n] || mpb[i%m]){
mpa[i%n]=1;
mpb[i%m]=1;
}
}
ll fl1=0,fl2=0;
rep(i,n){
if(mpa[i]) fl1++;
}
rep(i,m){
if(mpb[i]) fl2++;
}
// cout<<fl1<<" "<<fl2;
if(fl1==n && fl2==m) cout<<"YES\n";
else cout<<"NO\n";
}
int main()
{
fast();
solve();
// Test
// {
// solve();
// }
return 0;
}
1144D - Equalize Them All | 298A - Snow Footprints |
1753B - Factorial Divisibility | 804A - Find Amir |
1541C - Great Graphs | 607B - Zuma |
30A - Accounting | 959C - Mahmoud and Ehab and the wrong algorithm |
1215A - Yellow Cards | 237B - Young Table |
1216D - Swords | 271D - Good Substrings |
573A - Bear and Poker | 10A - Power Consumption Calculation |
1244B - Rooms and Staircases | 777A - Shell Game |
1698D - Fixed Point Guessing | 415B - Mashmokh and Tokens |
26D - Tickets | 471B - MUH and Important Things |
982B - Bus of Characters | 1102B - Array K-Coloring |
818A - Diplomas and Certificates | 70A - Cookies |
798A - Mike and palindrome | 1690F - Shifting String |
366B - Dima and To-do List | 120B - Quiz League |
740A - Alyona and copybooks | 1491A - K-th Largest Value |